#include "CustomVerticalHeader.h"
#include <QEvent>

CustomVerticalHeader::CustomVerticalHeader(Qt::Orientation orientation, QWidget* parent)
    : QHeaderView(orientation, parent), m_selectedIcon(":/Resource/action_icon.png"), m_selectedSection(-1)
{
    setSectionsClickable(true); // 确保表头可点击
    setSectionsMovable(false);  // 不需要移动行，仅调整宽度
    setSectionResizeMode(QHeaderView::Interactive);
}

void CustomVerticalHeader::setEditRow(int row) {
    if (m_editSection != row) {
        m_editSection = row;
        viewport()->update(); // 触发表头重绘
    }
}


void CustomVerticalHeader::paintEvent(QPaintEvent* event)
{
    // 1. 先调用基类绘制默认表头（包括列/行标签）
    QHeaderView::paintEvent(event);

    QPainter painter(viewport());

    // 2. 仅填充空白区域（未包含列的部分）
    int lastSectionPos = 0;
    if (count() > 0) {
        lastSectionPos = sectionPosition(count() - 1) + sectionSize(count() - 1);
    }

    QRect blankRect;
    if (orientation() == Qt::Horizontal) {
        // 水平表头：填充右侧空白
        blankRect = QRect(lastSectionPos, 0,
            viewport()->width() - lastSectionPos,
            viewport()->height());
    }
    else {
        // 垂直表头：填充底部空白
        blankRect = QRect(0, lastSectionPos,
            viewport()->width(),
            viewport()->height() - lastSectionPos);
    }

    if (!blankRect.isEmpty()) {
        painter.fillRect(blankRect, Qt::red);
    }
}

void CustomVerticalHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const {
    // 获取表头文本
    // 获取表头文本
    QString text = model()->headerData(logicalIndex, orientation()).toString();

    // 设置背景颜色（选中时更深）
    QColor backgroundColor;
    if (logicalIndex == m_selectedSection) {
        backgroundColor = QColor("#005A9E"); // 选中时深蓝色
    } else if (logicalIndex == m_hoverSection) {
        backgroundColor = QColor("#ff0000"); // 悬停时的颜色
         painter->fillRect(rect, QBrush(backgroundColor));
    } else {
        backgroundColor = QColor("#0078D7"); // 默认浅蓝色
    }

    
    painter->fillRect(rect, QBrush(backgroundColor));


    // 绘制分界线（右侧边框）
    painter->setPen(QColor("#FFFFFF")); // 白色分界线
    painter->drawLine(rect.bottomLeft(), rect.bottomRight());

    // 设置字体颜色为白色
    painter->setPen(Qt::white);
    painter->drawText(rect, Qt::AlignCenter, text); // 居中绘制文本

    // 如果列被选中，绘制图标
    if (logicalIndex == m_selectedSection) {
        m_selectedIcon.paint(painter, rect, Qt::AlignRight | Qt::AlignVCenter);
    } else if(m_editSection == logicalIndex) {
        painter->save();
        painter->fillRect(rect, QColor("#0000ff")); // 蓝色背景
        painter->setPen(Qt::white);
        painter->drawText(rect, Qt::AlignCenter, QString::number(logicalIndex + 1)); // 绘制行号
        painter->restore();
    }

}

void CustomVerticalHeader::mousePressEvent(QMouseEvent* event) {
    QHeaderView::mousePressEvent(event); // 调用基类方法以保持默认行为

    int logicalIndex = logicalIndexAt(event->pos()); // 获取点击的列索引
    if (logicalIndex >= 0) {
        m_selectedSection = logicalIndex; // 更新选中的表头部分
        viewport()->update(); // 强制更新视图以重新绘制表头

        QTableView* tableView = qobject_cast<QTableView*>(parent());
        if (tableView) {
            tableView->selectRow(logicalIndex); // 选中整列
        }
    }
}

void CustomVerticalHeader::mouseMoveEvent(QMouseEvent* event)
{
    int newHoverSection = logicalIndexAt(event->pos());
    if (newHoverSection != m_hoverSection) {
        m_hoverSection = newHoverSection; // 更新悬停列索引
        viewport()->update(); // 触发重绘
    }
}

void CustomVerticalHeader::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    if (m_hoverSection != -1) {
        m_hoverSection = -1; // 鼠标离开时清除悬停状态
        viewport()->update(); // 触发重绘
    }
}
